<?php
/**
 * Created by PhpStorm.
 * User: xiemi
 * Date: 2020/4/1
 * Time: 19:24
 */

namespace darksun;


class Request
{
    public $app;
    private $realIP;
    private $get = [];
    private $post = [];
    private $input;
    private $session = [];
    private $cookie = [];
    private $server = [];
    private $file = [];
    private $header = [];
    protected $mimeType = [
        'xml'   => 'application/xml,text/xml,application/x-xml',
        'json'  => 'application/json,text/x-json,application/jsonrequest,text/json',
        'js'    => 'text/javascript,application/javascript,application/x-javascript',
        'css'   => 'text/css',
        'rss'   => 'application/rss+xml',
        'yaml'  => 'application/x-yaml,text/yaml',
        'atom'  => 'application/atom+xml',
        'pdf'   => 'application/pdf',
        'text'  => 'text/plain',
        'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*',
        'csv'   => 'text/csv',
        'html'  => 'text/html,application/xhtml+xml,*/*',
    ];

    public function __construct()
    {
        $this->input = file_get_contents('php://input');
        $this->server = $_SERVER;
        $this->get = $_GET;
        $this->post = $_POST;
        $this->file = $_FILES??[];
        $this->cookie = $_COOKIE;
        $this->realIP = $this->get_real_ip();

        if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
            $header = $result;
        } else {
            $header = [];
            $server = $_SERVER;
            foreach ($server as $key => $val) {
                if (0 === strpos($key, 'HTTP_')) {
                    $key          = str_replace('_', '-', strtolower(substr($key, 5)));
                    $header[$key] = $val;
                }
            }
            if (isset($server['CONTENT_TYPE'])) {
                $header['content-type'] = $server['CONTENT_TYPE'];
            }
            if (isset($server['CONTENT_LENGTH'])) {
                $header['content-length'] = $server['CONTENT_LENGTH'];
            }
        }
        $this->header = array_change_key_case($header);
    }

    public function bootService(App $app)
    {
        $this->app = $app;
        $app->request = $this;
    }

    public function get_real_ip()
    {
        $ip=FALSE;
        //客户端IP 或 NONE
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        //多重代理服务器下的客户端真实IP地址（可能伪造）,如果没有使用代理，此字段为空
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
            for ($i = 0; $i < count($ips); $i++) {
                if (!preg_match ("%^10\.|192\.168|172\.(1[6-9]|2|3[01])%", $ips[$i])) {
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        //客户端IP 或 (最后一个)代理服务器 IP
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }

    public function get($key = null, $value=null){
        if($value){
            $this->get[$key] = $value;
        }else{
            return $this->get[$key]??null;
        }
        return $this->get[$key];
    }

    public function post($key = null, $value=null){
        if(!$key) return $this->post;
        if($value){
            $this->post[$key] = $value;
        }else{
            return $this->post[$key]??null;
        }
        return $this->post[$key];
    }


    public function input($key = null, $value=null){
        if(!$key) return $this->input;
        if($value){
            $this->input[$key] = $value;
        }else{
            return $this->input[$key]??null;
        }
        return $this->input[$key];
    }
    
    public function server($key = null, $value=null){
        if(!$key) return $this->server;
        if($value){
            $this->server[$key] = $value;
        }else{
            return $this->server[$key]??null;
        }
        return $this->server[$key];
    }
}